import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '../views/Login.vue'
import MainBox from '../views/MainBox.vue'
import RoutesConfig from './config.js'
import store from '@/store'
const routes = [
  {
    path: "/login",
    name: "login",
    component: Login
  },
  {
    path: "/mainbox",
    name: "mainbox",
    component: MainBox
  } 
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

//路由拦截
router.beforeEach((to, from, next) => {
  if (to.name === "login") next()
  else {
    if (!localStorage.getItem("token")) {
      next({
        path: "/login"
      })
    } else {
      if (!store.state.isGetterRouter) {
        router.removeRoute("mainbox")
        ConfigRouter()
        next({
          path: to.fullPath
        })
      } else {
        next()
      }
    }
  }
})

const ConfigRouter = () => {
  if(!router.hasRoute("mainbox")){
    router.addRoute({
      path: "/mainbox",
      name: "mainbox",
      component: MainBox
    })
  }
  RoutesConfig.forEach(element => {
    checkPermission(element) && router.addRoute("mainbox", element)
  })

  store.commit("changeGetterRouter",true)
}

const checkPermission = (element)=>{
  if(element.requireAdmin){
    return store.state.userInfo.role === 1
  }
  return true
}

export default router
